package org.ufinity.hsms.module.panel;

import org.json.JSONException;
import org.json.JSONObject;
import org.ufinity.hsms.R;
import org.ufinity.hsms.common.activity.BaseActivity;
import org.ufinity.hsms.common.application.HSMApplication;
import org.ufinity.hsms.common.command.AbstractCommand;
import org.ufinity.hsms.common.command.CommandService;
import org.ufinity.hsms.common.command.ICallBack;
import org.ufinity.hsms.common.utils.InfoHelper;
import org.ufinity.hsms.common.utils.SystemUtils;
import org.ufinity.hsms.module.device.command.HostGroupCommand;
import org.ufinity.hsms.module.panel.adapter.PanelAdapter;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class PanelActivity extends BaseActivity implements OnItemClickListener{
	public static final String TAG = "PanelActivity";
	private HSMApplication application;
	ProgressDialog proDialog = null;
	ListView listView;
	PanelAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		instance = this;
		application = getBaseApplication();
		application.addActivity(instance);
		setContentView(R.layout.panel);
		initAapter();
		initView();
		initDate();
	}

	private void initAapter() {
		adapter = new PanelAdapter(this);
	}
	
	/**
	 * 初始化控件
	 */
	private void initView() {
		listView = (ListView) findViewById(R.id.listView);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		
		proDialog = new ProgressDialog(getParent());
		proDialog.setMessage(getResources().getString(R.string.validate));
		proDialog.setIndeterminate(false);
		proDialog.setCancelable(true);
	}

	private void initDate() {
		if (!InfoHelper.checkNetWork(mContext)) {
			makeMyToast(mContext, getResources().getString(
					R.string.connect_error));
			return;
		}else{
			proDialog.show();
			HostGroupCommand cmd = new HostGroupCommand(true);
			cmd.addCallBack("hostGroup",new ICallBack<JSONObject>() {
				
				@Override
				public void callBack(AbstractCommand<JSONObject> command,
						JSONObject json, Exception ex) {
					if (json!=null && !json.isNull("result"))
					{
						String result;
						try {
							result = json.getString("result");
							
							/*List<TriggerEntity> list = TriggerEntity.getList(result);
							if(list!=null && list.size()>0){
								int size = list.size();
								String hostId = "";
								for(int i = 0;i<size;i++){
									if(i == 0){
										hostId = list.get(i).getGroupid();
									}else{
										hostId = hostId + "," + list.get(i).getGroupid();
									}
								}
							}
							
							adapter.setItems(list);
							adapter.notifyDataSetChanged();
							// 设置选中
							listView.setSelection(0);*/
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}else{
						makeMyToast(instance, "");
					}
					
					if(proDialog!=null){
						proDialog.dismiss();
					}
				}
			});
			CommandService.addTaskAtFirst(cmd);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
	}

	/**
	 * 锁back键 控制提示退出
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			SystemUtils.openOptionsDialog(getParent(), application);
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
}
